﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

public partial class adm_sc_categories_Process : System.Web.UI.Page
{
    public int CategoryID
    {
        get
        {
            if (ViewState["CategoryID"] != null)
                return (int)ViewState["CategoryID"];
            else
                return 0;
        }
        set
        {
            ViewState["CategoryID"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {           
            photoview.Visible = false;
            txt_priority.Text = "0";
            if (Request.Params["cid"] != null)
            {
                
                this.CategoryID = Convert.ToInt32(Request.Params["cid"]);
                Load_Information();

                
            }
            CategoriesBLL.BindCategories(drp_parent, 0, Convert.ToInt32(drp_type.SelectedValue), " No Parent ", "0",true);
        }
    }

    private void Load_Information()
    {
        List<Category_Struct_V2> _lst = CategoriesBLL.Load_Categories("", this.CategoryID, 0, 0, 0, "");
        if (_lst.Count > 0)
        {
            txt_categoryname.Text = _lst[0].CategoryName;
            txt_term.Text = _lst[0].Term;
            txt_priority.Text = _lst[0].Priority.ToString();
            drp_type.SelectedValue = _lst[0].Type.ToString();
            drp_mode.SelectedValue = _lst[0].Mode.ToString();
            drp_parent.SelectedValue = _lst[0].ParentID.ToString();
            drp_status.SelectedValue = _lst[0].isPrivate.ToString();
            lbl_filename.Text = _lst[0].PictureName;
            if (_lst[0].PictureName != "none")
            {
                photoview.Visible = true;
                img.ImageUrl = Config.GetUrl("contents/category/" + _lst[0].PictureName);
            }
        }
    }
    protected void btn_add_Click(object sender, EventArgs e)
    {
        int type = Convert.ToInt32(drp_type.SelectedValue);
        // Add category record in database
        Category_Struct_V2 cat = new Category_Struct_V2();
        bool isUpdate = false;
        if (this.CategoryID > 0)
            isUpdate = true;
        cat.CategoryID = this.CategoryID;
        cat.CategoryName = txt_categoryname.Text;
        cat.Term = txt_term.Text;
        cat.ParentID = Convert.ToInt32(drp_parent.SelectedValue);
        cat.Mode = Convert.ToInt32(drp_mode.SelectedValue);
        cat.isPrivate = Convert.ToInt32(drp_status.SelectedValue);
        cat.Priority = Convert.ToInt32(txt_priority.Text);
        cat.Type = Convert.ToInt32(drp_type.SelectedValue);

        cat.Description = "";

        CategoriesBLL.Process(cat,isUpdate);
        if(isUpdate)
            Response.Redirect(Config.GetUrl("adm/sc/categories/Default.aspx?status=updated"));
        else
            Response.Redirect(Config.GetUrl("adm/sc/categories/Default.aspx?status=added"));
    }

    protected void drp_type_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        // load categories based on selected type
        CategoriesBLL.BindCategories(drp_parent, 0, Convert.ToInt32(drp_type.SelectedValue), " No Parent ", "0",true);
    }

    

}